package com.ioc.business.base.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ioc.business.base.domain.CustomerInfo;
import com.ioc.business.base.domain.bo.customer.CustomerInfoBo;
import com.ioc.business.base.mapper.CustomerInfoMapper;
import com.ioc.business.base.service.ICustomerInfoService;
import com.ioc.business.base.vo.customer.info.CustomerInfoItemVo;
import com.ioc.business.order.domain.bo.plan.arrival.GlobalCustomerSetBo;
import com.ioc.business.order.domain.bo.plan.arrival.item.PlanArrivalOrderItemBo;
import com.ioc.business.order.service.IPlanArrivalOrderService;
import com.ioc.common.constant.UserConstants;
import com.ioc.common.exception.ServiceException;
import com.ioc.common.utils.CollectionUtils;
import com.ioc.common.utils.SecurityUtils;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 客户信息表 服务实现类
 * </p>
 *
 * @author WeiZhongRong
 * @since 2025-01-09
 */
@Service
@AllArgsConstructor
public class CustomerInfoServiceImpl extends ServiceImpl<CustomerInfoMapper, CustomerInfo> implements ICustomerInfoService {

    private final IPlanArrivalOrderService planArrivalOrderService;

    @Override
    public List<CustomerInfoItemVo> getCustomerInfoList() {
        List<CustomerInfo> list = new LambdaQueryChainWrapper<>(baseMapper)
                .eq(CustomerInfo::getDelFlag, UserConstants.DEL_FLAG_NORMAL)
                .list();
        if (CollectionUtil.isEmpty(list)) {
            return new ArrayList<>(0);
        }
        return BeanUtil.copyToList(list, CustomerInfoItemVo.class);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean editCustomerInfo(List<CustomerInfoBo> boList) {
        List<String> customerNameList = boList.stream().map(CustomerInfoBo::getCustomerName).collect(Collectors.toList());
        if (CollectionUtil.isEmpty(customerNameList)) {
            throw new ServiceException("客户名称不能为空");
        }
        List<String> duplicates = CollectionUtils.findDuplicates(customerNameList);
        if (CollectionUtil.isNotEmpty(duplicates)) {
            throw new ServiceException("客户名称【" + String.join(StrUtil.COMMA, duplicates) + "】存在重复！");
        }
        String username = SecurityUtils.getUsername(); // 获取当前用户名
        Date date = new Date(); // 获取当前日期
        List<CustomerInfo> customerInfoList = new ArrayList<>(boList.size());
        List<CustomerInfoBo> updateList =
                boList.stream().filter(bo -> ObjectUtil.isNotEmpty(bo.getCustomerId())).collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(updateList)) {
            List<CustomerInfo> customerInfos = buildCustomerInfo(false, updateList, username, date);
            updateBatchById(customerInfos);
            customerInfoList.addAll(customerInfos);
        }
        List<CustomerInfoBo> saveList =
                boList.stream().filter(bo -> ObjectUtil.isEmpty(bo.getCustomerId())).collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(saveList)) {
            List<CustomerInfo> customerInfos = buildCustomerInfo(true, saveList, username, date);
            saveBatch(customerInfos);
            customerInfoList.addAll(customerInfos);
        }
        if (CollectionUtil.isNotEmpty(customerInfoList)) {
            GlobalCustomerSetBo globalCustomerSetBo = new GlobalCustomerSetBo();
            List<PlanArrivalOrderItemBo> collect = customerInfoList.stream().map(customerInfo -> {
                PlanArrivalOrderItemBo planArrivalOrderItemBo = new PlanArrivalOrderItemBo();
                planArrivalOrderItemBo.setCustomerId(customerInfo.getCustomerId());
                planArrivalOrderItemBo.setCustomerName(customerInfo.getCustomerName());
                return planArrivalOrderItemBo;
            }).collect(Collectors.toList());
            globalCustomerSetBo.setCustomerInfoList(collect);
            planArrivalOrderService.setCustomerInfo(globalCustomerSetBo);
        }
        return true;
    }

    public List<CustomerInfo> buildCustomerInfo(boolean save, List<CustomerInfoBo> list, String username, Date date) {
        return list.stream().map(i -> {
            CustomerInfo customerInfo = new CustomerInfo();
            BeanUtils.copyProperties(i, customerInfo);
            if (save) {
                customerInfo.setCreateBy(username);
                customerInfo.setCreateTime(date);
            }
            customerInfo.setUpdateBy(username);
            customerInfo.setCreateTime(date);
            return customerInfo;
        }).collect(Collectors.toList());
    }
}
